<template>
  <div class="main">
    <myForm
      ref="myForm"
      :dataForm="dataForm"
      :formItem="formItem"
      :confimText="'确认'"
      @cancel="cancel"
      @handleSubmit="handleSubmit"
    />
  </div>
</template>

<script>
import myForm from '../../../views/record/components/myForm.vue';
import { updatePwd } from '@/api/user.js';
import { Md5 } from 'ts-md5/dist/md5';

export default {
  name: 'userInfo',
  components: { myForm },
  props: {
    dataForm: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      formItem: [
        {
          key: 'password',
          label: '旧密码',
          propsType: 'password'
        },
        {
          key: 'newPassword',
          label: '新密码',
          propsType: 'password'
        },
        {
          key: 'confirmPassword',
          label: '确认新密码',
          propsType: 'password'
        }
      ]
    };
  },
  methods: {
    handleSubmit(values) {
      const { password, newPassword, confirmPassword } = values;
      const params = {
        password: Md5.hashStr(password),
        newPassword: Md5.hashStr(newPassword),
        confirmPassword: Md5.hashStr(confirmPassword)
      };
      updatePwd(params).then(res => {
        if (res.code == 0) {
          this.$message.success('修改成功，即将跳转登陆页');
          this.$emit('cancel');
          setTimeout(() => {
            this.$router.push({
              path: '/login'
            });
          }, 1500);
        }
      });
    },
    cancel() {
      this.$emit('cancel');
    },
    resetFields() {
      this.$refs.myForm.resetFields();
    }
  }
};
</script>

<style scoped lang="scss"></style>
